package com.dede.tabanim

import android.os.Build
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            actionBar?.elevation = 0f
            supportActionBar?.elevation = 0f
        }

        val tab1 = tab_layout.newTab()
        tab1.setIcon(R.drawable.ic_tab_alarm).contentDescription = "Alarm"
        tab_layout.addTab(tab1, 0)

        val tab2 = tab_layout.newTab()
        tab2.setIcon(R.drawable.ic_tab_clock)
        tab_layout.addTab(tab2, 1)

        val tab3 = tab_layout.newTab()
        tab3.setIcon(R.drawable.ic_tab_timer)
        tab_layout.addTab(tab3, 2)

        val tab4 = tab_layout.newTab()
        tab4.setIcon(R.drawable.ic_tab_stopwatch)
        tab_layout.addTab(tab4, 3)

        tab_layout.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(view_pager))

        view_pager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int) = TestFragment()

            override fun getCount() = 4
        }

        view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                tab_layout.getTabAt(position)!!.select()
            }
        })
    }
}
